package org.jsola.hr.service.impl;

import org.jsola.common.DateKit;
import org.jsola.contract.entity.ContractDO;
import org.jsola.contract.service.IContractPathService;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.service.IEmpInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author zhenghua
 */

@Service("hrContractPathServiceImpl")
public class ContractPathServiceImpl implements IContractPathService {

    @Autowired
    private IEmpInfoService empInfoService;


    @Override
    public String getFileName(ContractDO contractDO, String siteId) {

        return contractDO.getId() + "_" + DateKit.getFormatDate(new Date(), "yyyyMMddHHmmss") + ".pdf";
    }

    @Override
    public String getOriginalFilename(ContractDO contractDO, String siteId) {
        String idCard;
        List<EmpInfoDO> empInfoDOList = empInfoService.selectByPhonesAndSiteId(
                Collections.singletonList(contractDO.getPartyBPhone()), siteId);
        if (CollectionUtils.isEmpty(empInfoDOList)) {
            idCard = "身份证号未填写";
        } else {
            idCard = empInfoDOList.get(0).getIdCard() == null ? "身份证号未填写" : empInfoDOList.get(0).getIdCard();
        }
        return contractDO.getName() + "(" + idCard + ").pdf";
    }

}